import {Injectable} from "@angular/core";
import {HttpHelper} from "./HttpHelper";
import {TimeSheet} from "./domains";

@Injectable()
export class TimeSheetService{
    timeSheet:TimeSheet;
    constructor(private helper:HttpHelper){
    }
    signIn(processId:number){
        return this.helper.postToBoolean("time-sheet","process="+processId,this.helper.contentHeaderEnum.formHeader);
    }
    signOut(){
        return this.helper.putToBoolean("time-sheet",null);
    }

    signed():Promise<TimeSheet>{
        return this.helper.getJson<TimeSheet>("time-sheet/signed");
    }
    getTodaySignIn():Promise<TimeSheet[]> {
        return this.helper.getJson<TimeSheet[]>("time-sheet/signin");
    }
    workerTimeStart():Promise<TimeSheet> {
        return this.helper.postToJson<TimeSheet>("time-sheet/start",null);
    }
    endWorking(processTime) :Promise<TimeSheet> {
        let url = `time-sheet/end/?time=${this.timeSheet.id}`
        return this.helper.putToJson<TimeSheet>(url,[processTime]);
    }
    getTimeSheets(page?,size?,worker?):Promise<TimeSheet[]> {
        let url = `time-sheet`;
        if (page&&size) {
            url = `time-sheet/?page=${page}&size=${size}`;
        }
        if (worker) {
            url = `time-sheet/?worker=${worker}`
        }
        if (page&&size&&worker) {
            url = `time-sheet/?page=${page}&size=${size}&worker=${worker}`;
        }
        return this.helper.getJson<TimeSheet[]>(url);
    }
}